Single Compilation Unit is a C/C++ technique which reduces compilation time and aids the compiler to perform program optimization even when the compiler itself is lacking support for whole program optimization or precompiled headers.
Usually a C/C++ development environment assumes that .c/.cpp source files are preprocessed (creating translation units) and translated separately by the compiler to object (.o or .obj) files, which can then be linked together to create an executable file or library. This leads to multiple passes being performed on common header files and, with C++, multiple template instantiations of the same templates in different translation units. The Single Compilation Unit technique uses pre-processor directives to "glue" different translation units together at compile-time, not link-time. This reduces the overall build time, but increases the build time required after making minor changes to any source file included in a Single Compilation Unit. So this technique is appropriate for some modules which consist of infrequently modified source files.
SCU also allows an optimizer to trace deeper relations between functions, therefore allowing optimizations such as inlining. It also helps avoiding implicit code bloat due to exceptions, side effects, and register allocation, which are generally overlooked by the classic scheme of using separate modules, and not always achieved by the use of precompiled headers.
For example, if you have the source files foo.cpp and bar.cpp, they can be placed in a Single Compilation Unit as follows:
#include "foo.cpp" #include "bar.cpp"
Suppose foo.cpp and bar.cpp are:
//foo.cpp #include <iostream> // A large, standard header #include "bar.h" // Declaration of function 'bar' int main() // Definition of function 'main' { bar(); }
//bar.cpp #include <iostream> // The same large, standard header void bar() // Definition of function 'bar' { ... }
Now the standard header file ('iostream') is compiled only once, and function 'bar' may be inlined into function 'main', despite being from another module.